//
//  BLClosthWashViewController.m
//  blanket
//
//  Created by Mac on 2017/12/7.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLClosthWashViewController.h"
#import "BLWashCollectionViewCell.h"
#import "BLWashModel.h"
#import "BLShopCarViewController.h"
#import "BLInventoryViewController.h"
@interface BLClosthWashViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) NSMutableArray                     *washMenuButtons;//床单等
@property(nonatomic, strong) NSMutableArray                     *washTypeButtons;//洗涤，重污染等
@property(nonatomic, strong) UICollectionView                     *collectionView;

@property(nonatomic, strong) NSArray                     *washMenuArray;//网络数据 被套 床单 枕套
@property(nonatomic, strong) NSArray                     *washTypeArray;//网络数据,洗涤，重污染
@property(nonatomic, strong) NSMutableArray                     *dataSouce;

@property(nonatomic, strong) STButton                     *shopButton;//购物车
@property(nonatomic, strong) STLabel                      *moneyLable;//金额
@property(nonatomic, strong) STButton                     *cofrimButton;//购买

@property(nonatomic, strong) NSURLSessionDataTask                     *task;
@property(nonatomic, strong) STNoresultView                     *noresultView;

@property(nonatomic, strong) NSMutableArray<BLWashModel*>                     *fecthArray;//取件array
@end

@implementation BLClosthWashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"布草洗涤";
    self.fecthArray = [NSMutableArray new];
    [self sendMenuRequest];
    
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, [TMUtils navigationBar].bottom, 200, 44)
                                                     text:@"请选择需要洗送的布草"
                                                textColor:BL_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self.view addSubview:titleLable];
    __weak typeof(self) weakSelf =  self;
    //配置menu
    CGFloat menuTop = titleLable.bottom;
    CGFloat typeLeft = 0;
    NSArray * menuTitleArray = self.washMenuArray;
    self.washMenuButtons = [NSMutableArray new];
    for (int i = 0; i < menuTitleArray.count; i ++) {
        NSDictionary * dic = menuTitleArray[i];
        
        NSString * title = @"";
        if ([dic.allKeys containsObject:@"name"]) {
            title =  dic[@"name"];
        }
        STButton * menuButton = [[STButton alloc] initWithFrame:CGRectMake(10, menuTop, 70, 30)
                                                          title:title
                                                     titleColor:BL_secendTextColor
                                                      titleFont:14
                                                   cornerRadius:0
                                                backgroundColor:BL_backgroundColor
                                                backgroundImage:nil
                                                          image:nil];
        [menuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        if (i == 0) {
            menuButton.backgroundColor = BL_BlueBackGroundColor;
            menuButton.selected = YES;
        }
        [menuButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedMenuButton:sender];
        }];
        menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        menuButton.tag = 10000 + i;
        [self.washMenuButtons addObject:menuButton];
        [self.view addSubview:menuButton];
        menuTop = menuTop + 30;
        typeLeft = menuButton.right + 10;
    }
    //配置type
    
    NSArray * typeTitleArray = @[@"洗涤",@"重污染",@"返洗",@"报损"];
    self.washTypeButtons = [NSMutableArray new];
    CGFloat buttonLeft = (UIScreenWidth - 10 - typeLeft )/4;
    for (int i = 0; i < typeTitleArray.count; i ++) {
        STButton * typeButton = [[STButton alloc] initWithFrame:CGRectMake(typeLeft + buttonLeft*i, titleLable.bottom, 45, 20)
                                                          title:typeTitleArray[i]
                                                     titleColor:BL_secendTextColor
                                                      titleFont:12
                                                   cornerRadius:5
                                                backgroundColor:[UIColor whiteColor]
                                                backgroundImage:nil
                                                          image:nil];
        [typeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        if (i == 0) {
            typeButton.backgroundColor = BL_BlueBackGroundColor;
            typeButton.selected = YES;
        }else{
            [typeButton st_setBorderWith:1 borderColor:BL_lineColor cornerRadius:5];
        }
        typeButton.hidden = YES;
        typeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        typeButton.tag = 20000 + i;
        [typeButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedTypeButton:sender];
        }];
        [self.washTypeButtons addObject:typeButton];
        [self.view addSubview:typeButton];
    }

    [self configBootomeView];
    //collectionView
    CGRect frame = CGRectMake(typeLeft, titleLable.bottom + 30, UIScreenWidth - 10 - 90, UIScreenHeight - [TMUtils tabbar].height - 50 - (titleLable.bottom + 30));
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flow];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.collectionView registerClass:[BLWashCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoodListRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
    
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 60, self.collectionView.width, 300)
                                                        title:@"抱歉,暂无该类商品"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noresultView.hidden = YES;
    [self.collectionView addSubview:self.noresultView];
    
    
    
    [self sendTypeRequest];
}
- (void)configBootomeView{
    UIView  * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth,[TMUtils tabbar].height)];
    bootomView.top = [TMUtils tabbar].top;
    bootomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bootomView];
    [bootomView st_showTopShadow];
    [bootomView st_showBottomLine];
    
    //底部
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(0, UIScreenHeight - 100, UIScreenWidth, 50)
                                                    text:@"请先选中商品，当出现红色选定框后，再修改数量"
                                               textColor:BL_redColor
                                                    font:15
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    nameLable.bottom = bootomView.top;
    [self.view addSubview:nameLable];
    
    __weak typeof(self) weakSelf =  self;
    self.shopButton = [[STButton alloc] initWithFrame:CGRectMake(10, 0, 44, 49)
                                                title:@""
                                           titleColor:KL_firstTextColor
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:nil
                                      backgroundImage:nil
                                                image:[UIImage imageNamed:@"购物车"]];
    self.shopButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self.shopButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedShopCarButton];
    }];
  //  [bootomView addSubview:self.shopButton];
    
    self.cofrimButton = [[STButton alloc] initWithFrame:CGRectMake(0, 7, 80, 35)
                                                  title:@"立即取件"
                                             titleColor:[UIColor whiteColor]
                                              titleFont:15
                                           cornerRadius:8
                                        backgroundColor:KL_BlueBackGroundColor
                                        backgroundImage:nil
                                                  image:nil];
    self.cofrimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cofrimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton];
    }];
    self.cofrimButton.right = UIScreenWidth - 10;
    [bootomView addSubview:self.cofrimButton];
    
    self.moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(self.shopButton.right + 10, 0, self.cofrimButton.left - self.shopButton.right - 20, 49)
                                                text:@"合计:$0"
                                           textColor:KL_redColor
                                                font:13
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentRight];
    self.moneyLable.attributedText = [self.moneyLable.text st_convertAttributeStringWithKeyWord:@"合计:"
                                                                                     attributes:@{NSForegroundColorAttributeName:KL_firstTextColor}];
    [bootomView addSubview:self.moneyLable];
    
}
- (void)refreshTYpeButtons{
    if (self.washTypeArray.count == self.washTypeButtons.count) {
        for (int i = 0; i < self.washTypeButtons.count; i ++ ) {
            UIButton * buttn = self.washTypeButtons[i];
            buttn.hidden = NO;
            NSDictionary * dic = self.washTypeArray[i];
            if ([dic.allKeys containsObject:@"name"]) {
                NSString * name = dic[@"name"];
                [buttn setTitle:name forState:UIControlStateNormal];
            }
        }
    }
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.collectionView.width/2 - 5, 135);//3/2 didChange 取消加入购物车
   // return CGSizeMake(self.collectionView.width/2 - 5, 175);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BLWashCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    BLWashModel * washModel = self.dataSouce[indexPath.row];
    washModel.indexPath = indexPath;
    for (BLWashModel * existModel in self.fecthArray) {
        if ([existModel.washID isEqualToString:washModel.washID]) {
            washModel.isSelected = existModel.isSelected;
            washModel.num = existModel.num;
        }
    }
    item.model = washModel;
    if (item.model.isSelected) {
        [item st_setBorderWith:1 borderColor:FlatRed cornerRadius:5];
    }else{
       [item st_setBorderWith:1 borderColor:BL_lineColor cornerRadius:5];
    }
    __weak typeof(self) weakSelf =  self;
    [item.addButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedAddCarButton:indexPath];
    }];
    [item.autoAddView setAutoAddViewNumChanged:^(NSInteger num) {
        [weakSelf onChangedValueAutoAddViewWithIndexPath:indexPath num:num];
    }];
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BLWashModel * model = self.dataSouce[indexPath.row];
    model.isSelected = !model.isSelected;
    if (model.isSelected) {
        if (![self.fecthArray containsObject:model]) {
            [self.fecthArray addObject:model];
        }

    }else{
        if ([self.fecthArray containsObject:model]) {
            [self.fecthArray removeObject:model];
        }
    }
//    if ([model.cate_Name isEqualToString:@"更多"] && model.isSelected) {
//        [self showTextFiledAlertWithTitle:@"请输入备注" message:@"" leftTitle:@"取消" rightTitle:@"确认" block:^(NSString *buttonTitle, NSString *textFiledText) {
//            if ([buttonTitle isEqualToString:@"确认"]) {
//                model.note = textFiledText;
//            }
//        }];
//    }
    [self.collectionView reloadData];
    [self cacluteCurrentAllMoney];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSlectedShopCarButton{
    [self.navigationController pushViewController:[BLShopCarViewController new] animated:YES];
}
- (void)onSelctedTypeButton:(UIButton*)sender{
    
    for (UIButton * button in self.washTypeButtons) {
        button.selected = NO;
        if (button == sender) {
            sender.selected = YES;
            
        }
        if (button.selected) {
            button.backgroundColor = BL_BlueBackGroundColor;
            [button st_setBorderWith:0 borderColor:BL_secendTextColor cornerRadius:5];
        }else{
            button.backgroundColor = [UIColor whiteColor];
            [button st_setBorderWith:1 borderColor:BL_lineColor cornerRadius:5];
        }
    }
    
    [self sendGoodListRequest];
}
- (void)onSelctedMenuButton:(UIButton*)sender{
    for (UIButton * button in self.washMenuButtons) {
        button.selected = NO;
        if (button == sender) {
            sender.selected = YES;
        }
        if (button.selected) {
            button.backgroundColor = BL_BlueBackGroundColor;
        }else{
            button.backgroundColor = BL_backgroundColor;
        }
    }
     [self sendGoodListRequest];
}

- (void)onSelectedConfimButton{
    if (!self.fecthArray.count) {
        [SVProgressHUD showErrorWithStatus:@"您还未请选择任何商品"];
        return;
    }
    BLInventoryViewController * vc = [BLInventoryViewController new];
    vc.dataSouce = [self.fecthArray copy];
    [self.navigationController pushViewController:vc animated:YES];
    return;
    [self showAlertWithTitle:[NSString stringWithFormat:@"您将盖币 支付%@",self.moneyLable.text]
                  titleColor:KL_firstTextColor
                     message:@"确认支付吗？"
                messageColor:BL_redColor
                   leftTitle:@"取消"
              leftTitleColor:KL_firstTextColor
                  rightTitle:@"确认"
             rightTitleColor:BL_BlueBackGroundColor
                      handle:^(NSString *name) {
                          if ([name isEqualToString:@"确认"]) {
                              NSDictionary * dic = [self fetchCurrentDic];
                              [self fetchMutipleGoddsConfimRequestWithDic:dic];
                          }
                      }];

}
- (void)onSelctedAddCarButton:(NSIndexPath*)indexPath{

    BLWashModel * mdoel = self.dataSouce[indexPath.row];
    [self sendAddToCarShopRequestWithModel:mdoel];
}
- (void)onChangedValueAutoAddViewWithIndexPath:(NSIndexPath*)indexPath num:(NSInteger)num{
    BLWashModel *  model = self.dataSouce[indexPath.row];
    model.num = @(num).description;
    [self cacluteCurrentAllMoney];
}
#pragma mark --Private Method
- (BLWashModel*)modelFormSelected{
    for (BLWashModel * model in self.dataSouce) {
        if (model.isSelected) {
            return model;
        }
    }
    return nil;
}
- (NSString*)fetchCurrentMenuID{
    for (UIButton * button in self.washMenuButtons) {
        if (button.selected) {
            NSInteger index = [self.washMenuButtons indexOfObject:button];
            NSDictionary * dic =  self.washMenuArray[index];
            return dic[@"id"];
            break;
        }
    }
    return @"1";
}
- (NSString*)fetchCurrentTypeID{
    for (UIButton * button in self.washTypeButtons) {
        if (button.selected) {
            NSInteger index = [self.washTypeButtons indexOfObject:button];
            NSDictionary * dic =  self.washTypeArray[index];
            return dic[@"id"];
            break;
        }
    }
    return @"1";
}
- (NSString*)fetchMenuNameWithMenuID:(NSString*)menuId{
    for (NSDictionary * dic in self.washMenuArray) {

        if ([[dic[@"id"] description] isEqualToString:menuId]) {
            return dic[@"name"];
        }
    }
    return @"被套";
}
- (NSString*)fetchTypeNameWithTypeID:(NSString*)typeId{
    for (NSDictionary * dic in self.washTypeArray) {
        if ([[dic[@"id"] description] isEqualToString:typeId]) {
            return dic[@"name"];
        }
    }
    return @"被套";
}
- (void)reloadData{
    self.noresultView.hidden = self.dataSouce.count;
    [self cacluteCurrentAllMoney];
    [self.collectionView reloadData];
}
- (void)cacluteCurrentAllMoney{
    CGFloat money = 0;
    for (BLWashModel * model in self.fecthArray) {
        if (model) {
            money = money + model.price.floatValue * model.num.integerValue;
        }
    }
    self.moneyLable.text = [NSString stringWithFormat:@"合计:%0.2f",money];
    self.moneyLable.attributedText = [self.moneyLable.text st_convertAttributeStringWithKeyWord:@"合计:" attributes:@{NSForegroundColorAttributeName:KL_firstTextColor}];
}
- (NSMutableDictionary*)fetchCurrentDic{
    //    token     是     string     验证客户端是否合法的token     5fb0b61af324881e956b0e674376f97b
    //    uid     是     int     用户id
    //    card_id     是     int     购物车id     —多个用逗号拼接 如 1,2,3
    //    goods_intro     是     string     商品     —传json 列如：[{“gid”:”1”,”num”:”3”,”price”:”20”},{“gid”:”3”,”num”:”1”,”price”:”30”}]
    //    goods_type     是     int     类型     商品类型 1布草洗涤，2布草定制，3积分商城，4集采商城,5私人定制
    //    pay_type     是     int     支付方式     1,余额支付，2盖币加积分支付
    
    NSMutableDictionary  * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    NSMutableArray * cards = [NSMutableArray new];
    NSMutableArray * jsonArry = [NSMutableArray new];
    
    for (BLWashModel * model in self.fecthArray) {
//        [cards addObject:model.shopCarID];
        NSMutableDictionary * jsonDic = [NSMutableDictionary new];
        [jsonDic setObject:model.washID forKey:@"gid"];
        [jsonDic setObject:model.num forKey:@"num"];
        [jsonDic setObject:model.price forKey:@"price"];
        if (model.score.length) {
            [jsonDic setObject:model.score forKey:@"score"];
        }
        //只有洗涤，增加menuid he type id
        if ([self fetchCurrentMenuID]) {
            [jsonDic setObject:[self fetchCurrentMenuID] forKey:@"menu_id"];
        }
        if ([self fetchCurrentTypeID]) {
            [jsonDic setObject:[self fetchCurrentTypeID] forKey:@"cate_id"];
        }
        [jsonArry addObject:jsonDic];
    }
//    NSString * card_id = [cards componentsJoinedByString:@","];
//    if (card_id.length) {
//        [dic setObject:card_id forKey:@"card_id"];
//    }
    NSString * jsonString = jsonArry.mj_JSONString;
    [dic setObject:jsonString forKey:@"goods_intro"];
    NSInteger num = 1;
    [dic setObject:@(num) forKey:@"goods_type"];
    [dic setObject:@"4" forKey:@"pay_type"];
    return dic;
    
}
#pragma mark --NetWork Method
- (void)sendMenuRequest{

    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/cateAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                 [SVProgressHUD dismiss];
                                                 self.washMenuArray =  responseObject[@"data"];
                                                 [self configSubView];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);


                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                     [self sendMenuRequest];
                                                 });
                                             }];
}
- (void)sendTypeRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/getCharaAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 self.washTypeArray =  responseObject[@"data"];
                                                 [self refreshTYpeButtons];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                     [self sendTypeRequest];
                                                 });
                                             }];
}
- (void)sendGoodListRequest{
    [_task cancel];
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/washLinenAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }

    if ([self fetchCurrentMenuID]) {
        [dic setObject:[self fetchCurrentMenuID] forKey:@"menu_id"];
    }
    if ([self fetchCurrentTypeID]) {
        [dic setObject:[self fetchCurrentTypeID] forKey:@"cate_id"];
    }
    _task = [[STNetWrokManger defaultClient] taskRequestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * dataArray =  responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     BLWashModel  * model = [BLWashModel mj_objectWithKeyValues:modelDic];
                                                     model.washName = [self fetchTypeNameWithTypeID:dic[@"cate_id"]];
                                                     model.cate_Name = [self fetchMenuNameWithMenuID:dic[@"menu_id"]];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                  [self reloadData];
                                                  [self.collectionView.mj_header endRefreshing];

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 self.dataSouce = [NSMutableArray new];
                                                 [self reloadData];
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendAddToCarShopRequestWithModel:(BLWashModel*)model{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/shoppingCardAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    
    if (model.washID.length) {
        [dic setObject:model.washID forKey:@"gid"];
    }
    [dic setObject:@"1" forKey:@"type"];
    
    if (model.name.length) {
        [dic setObject:model.name forKey:@"name"];
    }
    if (model.price.length) {
        [dic setObject:model.price forKey:@"price"];
    }
    if (model.num.length) {
        [dic setObject:model.num forKey:@"num"];
    }

    NSString * menuID =  [self fetchCurrentMenuID];
     NSString * typeID =  [self fetchCurrentTypeID];
    
    if (menuID.length) {
        [dic setObject:menuID forKey:@"cate_id"];
    }
    if (typeID.length) {
        [dic setObject:typeID forKey:@"true_id"];
    }
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"添加到购物车成功"];
                                                 [self.fecthArray addObject:model];
                                                 self.shopButton.badgeValue = @(self.fecthArray.count).description;
                                                [self cacluteCurrentAllMoney];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//立即取件
- (void)sendFecthGoodsNowRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/takePickAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    NSMutableArray * idsArray = [NSMutableArray new];
    for (BLWashModel * model in self.fecthArray) {
        [idsArray addObject:model.washID];
    }
    NSString * gid = [idsArray componentsJoinedByString:@","];
    [dic setObject:gid forKey:@"gid"];
    
    NSString * price = [self.moneyLable.text stringByReplacingOccurrencesOfString:@"合计:" withString:@""];
    [dic setObject:price forKey:@"price"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"取件成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//新版多商品结算
- (void)fetchMutipleGoddsConfimRequestWithDic:(NSMutableDictionary*)dic{
    
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/multiShoppingCardAction"];
    NSString * type = dic[@"pay_type"];

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 NSString * order =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([type isEqualToString:@"1"]) {
                                                     //盖币加积分
                                                 }else if([type isEqualToString:@"2"]){
                                                     //支付宝
                                                     [TMUtils sendAliPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"3"]){
                                                     //微信
                                                     [TMUtils sendWechatPayWithOrder:order];
                                                     
                                                 }else if([type isEqualToString:@"4"]){
                                                     //余额
                                                     [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                                                     [self.navigationController popViewControllerAnimated:YES];
                                                    
                                                 }
                                                 
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

